package com.navi.util;


import com.navi.pojo.KeyValueForTimestampPojo;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 根据一段时间区间，按月份拆分成多个时间段
 * @author Sjz
 *
 */
public class SplitTimestampUtil {

    //@Test
    public void demo(){
        List<KeyValueForTimestampPojo> list = SplitTimestampUtil.getKeyValueForDate("2018-03-09 11:49:45.9", "2018-06-01 00:00:00.0");
        System.out.println("开始日期--------------结束日期");
        for(KeyValueForTimestampPojo date : list){
            System.out.println(date.getStartTiemstamp()+"-----"+date.getEndTimestamp());
        }
    }

    /**
     * 根据一段时间区间，按月份拆分成多个时间段
     * @param startTimestampString 开始日期
     * @param endTimestampString  结束日期
     * @return
     */
    public static List<KeyValueForTimestampPojo> getKeyValueForDate(String startTimestampString, String endTimestampString) {
        Timestamp startTimestamp = Timestamp.valueOf(startTimestampString);
        Timestamp endTimestamp = Timestamp.valueOf(endTimestampString);
        List<KeyValueForTimestampPojo> timestampList = null;
        try {
            timestampList = new ArrayList<KeyValueForTimestampPojo>();

            Calendar startCale = Calendar.getInstance();
            startCale.setTime(startTimestamp);

            Calendar endtCale = Calendar.getInstance();
            endtCale.setTime(endTimestamp);

            Calendar cale = Calendar.getInstance();

            int startDay = startTimestamp.getDate();
            int startHour = startTimestamp.getHours();
            int startMin = startTimestamp.getMinutes();
            int startSecond = startTimestamp.getSeconds();
            int startHourMinSecond = startHour*3600 + startMin*60 + startSecond*1;
            //System.out.println(startDay + " " + startHour + " " + startMin + " " + startSecond);

            int endDay = endTimestamp.getDate();
            int endHour = endTimestamp.getHours();
            int endMin = endTimestamp.getMinutes();
            int endSecond = endTimestamp.getSeconds();
            int endHourMinSecond = endHour*3600 + endMin*60 + endSecond*1;
            //System.out.println(endDay + " " + endHour + " " + endMin + " " + endSecond);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


            KeyValueForTimestampPojo  KeyValueForTimestampPojo = null;

            if(startCale.get(Calendar.MONTH) == endtCale.get(Calendar.MONTH) && startCale.get(Calendar.YEAR) == endtCale.get(Calendar.YEAR)) {
                KeyValueForTimestampPojo = new KeyValueForTimestampPojo();
                KeyValueForTimestampPojo.setStartTiemstamp(startTimestamp);
                KeyValueForTimestampPojo.setEndTimestamp(endTimestamp);
                timestampList.add(KeyValueForTimestampPojo);
                return timestampList;
            }

            while (startCale.getTime().before(endTimestamp)) {    // 判断是否到结束日期
                KeyValueForTimestampPojo = new KeyValueForTimestampPojo();
                cale.setTime(startCale.getTime());

                if(startCale.getTime().equals(startTimestamp)){
                    cale.set(Calendar.DAY_OF_MONTH, startCale.getActualMaximum(Calendar.DAY_OF_MONTH)); //取最后一天
                    cale.set(Calendar.HOUR_OF_DAY,23);
                    cale.set(Calendar.MINUTE,59);
                    cale.set(Calendar.SECOND,59);
                    cale.set(Calendar.MILLISECOND, 999);
                    KeyValueForTimestampPojo.setStartTiemstamp(startTimestamp);
                    KeyValueForTimestampPojo.setEndTimestamp(Timestamp.valueOf(sdf.format(cale.getTime())));

                }else if(startCale.get(Calendar.MONTH) == endtCale.get(Calendar.MONTH) && startCale.get(Calendar.YEAR) == endtCale.get(Calendar.YEAR)){
                    cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
                    cale.set(Calendar.HOUR_OF_DAY,0);
                    cale.set(Calendar.MINUTE,0);
                    cale.set(Calendar.SECOND,0);
                    cale.set(Calendar.MILLISECOND, 0);
                    KeyValueForTimestampPojo.setStartTiemstamp(Timestamp.valueOf(sdf.format(cale.getTime())));
                    KeyValueForTimestampPojo.setEndTimestamp(endTimestamp);
                }else {
                    cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
                    cale.set(Calendar.HOUR_OF_DAY,0);
                    cale.set(Calendar.MINUTE,0);
                    cale.set(Calendar.SECOND,0);
                    cale.set(Calendar.MILLISECOND, 0);
                    KeyValueForTimestampPojo.setStartTiemstamp(Timestamp.valueOf(sdf.format(cale.getTime())));

                    cale.set(Calendar.DAY_OF_MONTH, startCale.getActualMaximum(Calendar.DAY_OF_MONTH)); //取最后一天
                    cale.set(Calendar.HOUR_OF_DAY,23);
                    cale.set(Calendar.MINUTE,59);
                    cale.set(Calendar.SECOND,59);
                    cale.set(Calendar.MILLISECOND, 999);
                    KeyValueForTimestampPojo.setEndTimestamp(Timestamp.valueOf(sdf.format(cale.getTime())));
                }
                timestampList.add(KeyValueForTimestampPojo);
                startCale.add(Calendar.MONTH, 1);// 进行当前日期月份加1

            }

            if(endDay<startDay || (endDay==startDay && startHourMinSecond>=endHourMinSecond)){
                KeyValueForTimestampPojo = new KeyValueForTimestampPojo();
                cale.setTime(endTimestamp);
                cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
                cale.set(Calendar.HOUR_OF_DAY,0);
                cale.set(Calendar.MINUTE,0);
                cale.set(Calendar.SECOND,0);
                cale.set(Calendar.MILLISECOND, 0);
                KeyValueForTimestampPojo.setStartTiemstamp(Timestamp.valueOf(sdf.format(cale.getTime())));
                KeyValueForTimestampPojo.setEndTimestamp(endTimestamp);
                timestampList.add(KeyValueForTimestampPojo);
            }
        } catch (Exception e) {
            List<KeyValueForTimestampPojo> timestampList2 = new ArrayList<KeyValueForTimestampPojo>();
            KeyValueForTimestampPojo  keyValueForTimestamp2 = new KeyValueForTimestampPojo();
            keyValueForTimestamp2.setStartTiemstamp(startTimestamp);
            keyValueForTimestamp2.setEndTimestamp(endTimestamp);
            timestampList2.add(keyValueForTimestamp2);
            return timestampList2;
        }
        return timestampList;
    }

}

